package com.sun.example.widget;

import android.animation.Animator;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.sun.ui_lib.widget.BaseView;

/**
 * @author tianxiaolong
 * time：2019-08-22 21:37
 * description：
 */
public class AnimView  extends BaseView implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {


	private Paint mPaint;

	private Context mContext;

	private CircleWidget circleWidget;

	private ObjectAnimator mValueAnimator;
	public AnimView(Context context) {
		super(context);
	}

	public AnimView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
	}

	public AnimView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void init(Context context, AttributeSet attrs, int defStyleAttr) {
		super.init(context, attrs, defStyleAttr);
		mContext = context;
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(Color.BLACK);
        circleWidget = new CircleWidget(this);
        circleWidget.alpha = 255;
        circleWidget.centerY = 100;
        circleWidget.centerX = 100;
        circleWidget.radios = 100;

		setOnClickListener(v -> {

		});
		Keyframe keyframe1 = Keyframe.ofInt(0,0);
		Keyframe keyframe2 = Keyframe.ofInt(0.2f,200);
		Keyframe keyframe3 = Keyframe.ofInt(1,255);
		PropertyValuesHolder alpha = PropertyValuesHolder.ofKeyframe("alpha", keyframe1,keyframe2,keyframe3);
		PropertyValuesHolder centerX = PropertyValuesHolder.ofFloat("centerX", 100, 800);
		 mValueAnimator = ObjectAnimator.ofPropertyValuesHolder(circleWidget, alpha, centerX);
		 mValueAnimator.addListener(this);
		 mValueAnimator.setInterpolator(new BounceInterpolator());
		 mValueAnimator.setDuration(5000);




	}

	public void setTextScalX(float x){
		mPaint.setTextScaleX(x);
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {

//		mPaint.setAlpha( circleWidget.alpha);
//		canvas.drawCircle(circleWidget.centerX,circleWidget.centerY,circleWidget.radios,mPaint);
		mPaint.setFakeBoldText(true);
		mPaint.setTextSize(30);
       canvas.drawText("格拉那达",200,200,mPaint);

	}

	@Override
	public boolean performClick() {
//		if(mValueAnimator.isStarted()){
//			mValueAnimator.cancel();
//		}
//       mValueAnimator.start();
		return super.performClick();
	}

	@Override
	public void onAnimationUpdate(ValueAnimator valueAnimator) {
		  int value = (int) valueAnimator.getAnimatedValue();

		  invalidate();
	}

	@Override
	public void onAnimationStart(Animator animator) {

	}

	@Override
	public void onAnimationEnd(Animator animator) {
		release();
	}
    private void release(){
		circleWidget.radios = circleWidget.centerX = circleWidget.centerY = 100;
		circleWidget.alpha = 255;
		invalidate();
	}
	@Override
	public void onAnimationCancel(Animator animator) {
         release();
	}

	@Override
	public void onAnimationRepeat(Animator animator) {
         release();
	}


	public static class CircleWidget{
		private int alpha;
		private float radios;
		private float centerX;
		private float centerY;

		private View view;

		public CircleWidget(View view) {
			this.view = view;
		}

		public float getCenterY() {
			return centerY;
		}

		public int getAlpha() {
			return alpha;
		}

		public void setAlpha(int alpha) {
			Log.d("CirWidget","alpha:"+alpha);
			this.alpha = alpha;
			view.invalidate();
		}

		public float getRadios() {
			return radios;
		}

		public void setRadios(float radios) {
			this.radios = radios;
			view.invalidate();
		}

		public float getCenterX() {
			return centerX;
		}

		public void setCenterX(float centerX) {
			this.centerX = centerX;
			this.centerY = centerX;
			view.invalidate();
		}

	}


}
